laydate.skin('molv');
var currentDate = beginDate();

function beginDate() {
	var today = new Date();
	var y = today.getFullYear();
	var m = today.getMonth() + 1;
	if(m >= 1 && m <= 9) {
		m = "0" + m;
	}
	var strBeginTime = y + "-" + m + "-01 00:00:00";
	return strBeginTime;
}

var endDate = endTime();


function endTime() {
	var d = new Date();
	var vYear = d.getFullYear();
	var vMon = d.getMonth() + 1;
	var vDay = d.getDate();
	var h = d.getHours();
	var m = d.getMinutes();
	var se = d.getSeconds();
	var s = vYear + '-' + (vMon < 10 ? "0" + vMon : vMon) + '-' + (vDay < 10 ? "0" + vDay : vDay) + ' ' + (h < 10 ? "0" + h : h) + ':' + (m < 10 ? "0" + m : m) + ':' + (se < 10 ? "0" + se : se);
	return s;
}

$("#beginTime").val(currentDate);
$("#endTime").val(endDate);

var start = {
	elem: '#beginTime',
	format: 'YYYY-MM-DD hh:mm:ss',
	min: '2012-01-01', //设定最小日期为当前日期
	istime: true,
	istoday: false,
	choose: function(datas) {
		end.min = datas; //开始日选好后，重置结束日的最小日期
		end.start = datas //将结束日的初始值设定为开始日
	}
};
var end = {
	elem: '#endTime',
	format: 'YYYY-MM-DD hh:mm:ss',
	min: '2012-01-01',
	istime: true,
	istoday: false,
	choose: function(datas) {
		start.max = datas; //结束日选好后，重置开始日的最大日期
	}
};
laydate(start);
laydate(end);

var q = {
	//加载数据
	"loadData": function(data) {
		var objCount = data.returnData.length; //取得操作系统列表
		var htmlList = "";
		for(var i = 0; i < objCount; i++) {
			var optId = data.returnData[i].optId;
			var strLoginDate = data.returnData[i].insertTime;
			var userAccount = data.returnData[i].userAccount;
			var optChannel = data.returnData[i].optChannel;
			var optminChannel = data.returnData[i].optminChannel;
			var optName = data.returnData[i].optName;
			var optType = data.returnData[i].optType;
//			htmlList = htmlList + '<tr class="td"><td><span title='+userAccount+'>' + userAccount + '</span></td><td>' + optChannel + '</td><td><span title='+optName+'>' + optName + '</span></td><td>' + optType + '</td><td>' + strLoginDate.substr(0, 19) + '</td></tr>';
			htmlList = htmlList + '<tr class="td"><td><span title='+userAccount+'>' + userAccount + '</span></td><td>' + optChannel + '</td><td>' + optminChannel + '</td><td>' + optType + '</td><td>' + strLoginDate.substr(0, 19) + '</td></tr>';
		}
//		var strHead = "<tr class='th'><th width='200'>管理员名称</th><th width='200'>操作对象</th><th width='350'>操作内容</th><th width='200'>操作方式</th><th width='200'>操作时间</th></tr>";
		var strHead = "<tr class='th'><th width='200'>管理员名称</th><th width='200'>操作模块</th><th width='200'>子模块</th><th width='248'>操作动作</th><th width='250'>操作时间</th></tr>";
		var strPage = "<tr class='pages'><td colspan='5'><div class='ui_page' id='pageArea'></div></td></tr>"
		$(".ktab").html(strHead + htmlList + strPage);
	},
	//public load ajax
	"publicAjax": function(pIndex, numList, selectJson) {
		var url = basePath + "record/queryByParams.do?pageIndex=" + pIndex + "&pageSize=" + numList;
		var _obj = $(".ktab");
		$.ajax({
			type: "POST",
			url: url,
			data: selectJson,
			error: function(request) {
				layer.msg("加载失败，请刷新重试！", 2, -1);
			},
			success: function(data) {
				if(data.returnCode == 10000) {
					if(data.returnCode == 10000 && data.returnData != null && data.returnData.length != 0) {
						q.loadData(data);
						paging.loadPage(data);
					} else if(data.returnData == null || data.returnData.length == 0) {
						$(".ktab").html("<tr class='th'><th width='200'>管理员名称</th><th width='200'>操作模块</th><th width='200'>子模块</th><th width='248'>操作动作</th><th width='250'>操作时间</th></tr><tr class='td'><td colspan='4'>很遗憾，没有查询到相关数据！</td></tr>");
					}
				} else if(data.returnCode == 999994) {
					setUserTimeout(data);
				}
			},
			beforeSend: function() {
				var imgGif = "<p class='loadingGif'></p>";
				_obj.html(imgGif);
			}
		});
	}
};
//页面初始化加载数据
var beginPrick = $("#beginTime").val();
var endPrick = $("#endTime").val();

var selectJsonData = {
	startTime: beginPrick,
	endTime: endPrick,
};

q.publicAjax(1, numList, selectJsonData);
//导出execl
$('#excelBtn').live('click', function() {
	var s = $("#beginTime").val(),
		e = $("#endTime").val(),
		f = $("#keychannelId").val(),
		g = $("#keydongzId").val(),
		userName = $("#keyAdminId").val();
	var url = basePath + 'record/exportExcelForRecord?dongzname='+g+'&channelname='+f+'&startTime=' + s + '&endTime=' + e + '&userName=' + userName;
	if(s>e)
		{layer.msg("开始时间不能大于结束时间！", 2, -1);}
	else
		{window.open(url, '_blank');}
});
//点击查询
$("#selectBtn").live("click", function() {
	curPage = 1;
	qDataFun(1);
});
$('.enterSearch').keydown(function(e) {
	if(e.keyCode == 13) {
		curPage = 1;
		qDataFun(1);
	}
});

//点击触发换页的按钮
$("#pageArea .pNumBtn  a").live("click", function() {
	curPage = parseInt($(this).attr("toPage"));
	qDataFun(curPage);
});

//点击触发分批显示的按钮
$("#pageArea .columnsPage a").live("click", function() {
	numList = parseInt($(this).text());
	curPage = 1;
	qDataFun(curPage);
	subPage = $(this).index();
});
//跳转
$("#pageArea .gopg .btn").live("click", function() {
	curPage = parseInt($("#pageArea .gopg .txt").val());
	//判断输入的页数是否大于最大的页数
	if(curPage>countPage){
		curPage=countPage;
	}
	if(curPage>0){
		qDataFun(curPage);
	}
}); 

function qDataFun(curPages) {
	if($("#beginTime").val() > $("#endTime").val()&&("#beginTime").val() != "" && $("#endTime").val() != ""){
		layer.msg("开始时间不能大于结束时间！", 2, -1);}
	else 
	if(($("#beginTime").val() != "" && $("#endTime").val() != "")||($("#beginTime").val() == "" && $("#endTime").val() == "")) {
		beginPrick = $("#beginTime").val();
		endPrick = $("#endTime").val();
		name = $("#keyAdminId").val();
		channel = $("#keychannelId").val();
		dongz = $("#keydongzId").val();
		selectJsonData = null;
		selectJsonData = {
			userName: name,
			startTime: beginPrick,
			channelname: channel,
			endTime: endPrick,
			dongzname: dongz
		};
		q.publicAjax(curPages, numList, selectJsonData);
	} 
	else {
		layer.msg("开始时间和结束时间不能为空！", 2, -1);
	}
}

//时间转换时间戳
function transdate(endTime) {
	var date = new Date();
	date.setFullYear(endTime.substring(0, 4));
	date.setMonth(endTime.substring(5, 7) - 1);
	date.setDate(endTime.substring(8, 10));
	date.setHours(endTime.substring(11, 13));
	date.setMinutes(endTime.substring(14, 16));
	date.setSeconds(endTime.substring(17, 19));
	return Date.parse(date) / 1000;
}